package com.ys.web.background.controller;

import com.ys.utils.base.http.RequestHandler;
import com.ys.utils.base.http.ResponseHandler;
import com.ys.utils.base.j2se.StringHandler;
import com.ys.utils.spring.config.SystemError;
import com.ys.utils.spring.properties.Config;
import com.ys.web.background.service.BackgroundGroupService;
import com.ys.web.background.vo.BackgroundGroupListVo;
import com.ys.web.background.vo.GroupParamVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ 名称:
 * @ 创建人: dhf
 * @ 参数： null
 * @ 创建时间: 2022/5/10
 * @ 修改人和其它信息
 * @ 返回值:
 */
@Slf4j
@RestController
@RequestMapping("/manager/group")
public class BackgroundGroupController {

    @Value("${ys.default.head_img}")
    private String DEFAULT_IMG;

    @Autowired
    private BackgroundGroupService backgroundGroupService;

    @PostMapping("/list")
    public String getGroupList(HttpServletRequest request,GroupParamVo groupParamVo) {
        String ip2 = RequestHandler.getIP(request);
        if(!ip2.equals(Config.pp.get("ys.recharge.refund.ip"))){
            SystemError.wrapBs("400","此ip不在退款白名单中");
        }
        Map<String, Object> result = new HashMap<>();
        int count = backgroundGroupService.getGroupListCount(groupParamVo);
        result.put("dataSum", count);
        if (count > 0) {
            List<BackgroundGroupListVo> resultList = backgroundGroupService.getGroupList(groupParamVo);
            result.put("list", resultList);
        } else {
            result.put("list", new ArrayList<>());
        }
        return ResponseHandler.success(result);
    }


    @PostMapping("/freeze")
    public String freeze(HttpServletRequest request,String id) {
        String ip2 = RequestHandler.getIP(request);
        if(!ip2.equals(Config.pp.get("ys.recharge.refund.ip"))){
            SystemError.wrapBs("400","此ip不在退款白名单中");
        }
        if (StringHandler.isEmpty(id)) {
            return ResponseHandler.error("500", "参数传递错误");
        }
        backgroundGroupService.updateInfo(id, 1);
        return ResponseHandler.success();
    }

    @PostMapping("/unfreeze")
    public String unfreeze(HttpServletRequest request, String id) {
        String ip2 = RequestHandler.getIP(request);
        if(!ip2.equals(Config.pp.get("ys.recharge.refund.ip"))){
            SystemError.wrapBs("400","此ip不在退款白名单中");
        }
        if (StringHandler.isEmpty(id)) {
            return ResponseHandler.error("500", "参数传递错误");
        }
        backgroundGroupService.updateInfo(id, 0);
        return ResponseHandler.success();
    }

}
